<cfsetting enablecfoutputonly="true">
<cfswitch expression="#thisTag.executionMode#">
	<cfcase value="start">
		<cfparam name="attributes.coprocessorDirectory" default="">
		<cfparam name="attributes.coprocessorClassPath" default="">
		<cfif StructKeyExists(caller,'root')>
			<cfset root = caller.root>
		<cfelse>
			<cfset root=caller>
		</cfif>
		<cfif NOT thisTag.hasEndTag>
			<cfthrow detail="End tag required" message="All coop tags require an end tag." errorcode="sos.tag.attributes">
		</cfif>
		<cfif attributes.coProcessorDirectory NEQ '' AND attributes.coProcessorClassPath EQ ''>
			<cfthrow message="If you specify the coProcessorDirectory, you must also specify a coprocessorClassPath attribute">
		</cfif>
		<cfif attributes.coProcessorClassPath NEQ '' AND attributes.coProcessorDirectory EQ ''>
			<cfthrow message="If you specify the coProcessorClassPath, you must also specify a coprocessorDirectory attribute">
		</cfif>
		<cfscript>
		if(attributes.coprocessorDirectory EQ '' OR attributes.coprocessorClassPath EQ ''){
			if(isDefined("root._app.root.directory") OR isDefined("root._app.root.classPath")) {
				attributes.coprocessorDirectory = root._app.root.directory & root.attributes.controller;
				attributes.coprocessorClassPath = root._app.root.classPath & root.attributes.controller;
			} else {
				thisRequest.server.root.directory = left(cgi.PATH_TRANSLATED,len(cgi.PATH_TRANSLATED)-len(cgi.SCRIPT_NAME)+1);
				thisRequest.server.root.slash = right(thisRequest.server.root.directory,1);
				thisRequest.current.root.directory = getDirectoryFromPath(getBaseTemplatePath());
				thisRequest.current.root.path = replace(right(getDirectoryFromPath(getBaseTemplatePath()),len(getDirectoryFromPath(getBaseTemplatePath()))-len(thisRequest.server.root.directory)+1),"\","/","ALL");
				thisRequest.current.root.classPath = replace(mid(thisRequest.current.root.path,2,len(thisRequest.current.root.path)-1),"/",".","ALL");
				thisRequest.current.root.controller = getFileFromPath(getBaseTemplatePath());
				thisRequest.current.root.controller = left(thisRequest.current.root.controller,len(thisRequest.current.root.controller)-4);
				attributes.coProcessorDirectory = thisRequest.current.root.directory & thisRequest.current.root.controller;
				attributes.coProcessorClassPath = thisRequest.current.root.classPath & thisRequest.current.root.controller;
			}
		} else {
			thisRequest.server.root.directory = left(cgi.PATH_TRANSLATED,len(cgi.PATH_TRANSLATED)-len(cgi.SCRIPT_NAME)+1);
			thisRequest.server.root.slash = right(thisRequest.server.root.directory,1);
			thisRequest.current.root.directory = getDirectoryFromPath(thisRequest.server.root.slash & replace(attributes.coProcessorClassPath,".",thisRequest.server.root.slash,"all"));
			thisRequest.current.root.path = replace(right(getDirectoryFromPath(getBaseTemplatePath()),len(getDirectoryFromPath("/" & replace(attributes.coProcessorDirectory,"\","/","all")))-len(thisRequest.server.root.directory)+1),"\","/","ALL");
			thisRequest.current.root.classPath = replace(mid(thisRequest.current.root.path,2,len(thisRequest.current.root.path)-1),"/",".","ALL");
			thisRequest.current.root.controller = getFileFromPath("/" & replace(attributes.coProcessorClassPath,".","/","all"));
			thisRequest.current.root.controller = left(thisRequest.current.root.controller,len(thisRequest.current.root.controller));
		}

		thisRequest.current.root.coProcessorDirectory = attributes.coProcessorDirectory & ".cfc";
		thisRequest.current.root.coProcessorClassPath = attributes.coProcessorClassPath;
		thisRequest.server.shareDirectory = left(getDirectoryFromPath(getCurrentTemplatePath()),len(getDirectoryFromPath(getCurrentTemplatePath()))-10);
		/*
		thisRequest.server.sharePath = replace(right(thisRequest.server.shareDirectory,len(thisRequest.server.shareDirectory) - len(thisRequest.server.root.directory) + 2),"\","/","all");
		thisRequest.server.shareClassPath = right(replace(thisRequest.server.sharePath,"/",".","all"),len(thisRequest.server.sharePath) - 1);
		*/
		thisRequest.server.sharePath = "/share/";
		thisRequest.server.shareClassPath = "share.";

		if(! structKeyExists(root,"_API")){
			root._API = structNew();
		}
		if(! structKeyExists(root,"_init")){
			root._init = structNew();
		}
		/* basically this was a longhand way of testing for _postback,
		*** it is outside the scope of what COOP needs to do,
		*** so it has be removed and left to external framework
		*/
		// makes sure COOP is ready for no attributes
		if(structKeyExists(root,"attributes")){
			root._init.attributes = root.attributes;
		} else {
			root._init.attributes = {};
		}
		// makes sure COOP is ready for no API
		if(structKeyExists(root,"attributes")){
			API = root._API;
		} else {
			API = {};
		}
		if (NOT structKeyExists(root,"coop")) {
			if(fileExists(thisRequest.current.root.coprocessorDirectory)){
				root.coop = createObject("component","#thisRequest.current.root.coprocessorClassPath#");
			} else {
				root.coop = createObject("component","#thisRequest.server.shareClassPath#objects.coop.coop");
			}
			// makes sure COOP has sosContent object
			if(structKeyExists(root,"sos") && structKeyExists(root.sos,"bufferContent")){
				sosContent = root.sos.bufferContent;
			} else {
				sosContent = createObject("component","#thisRequest.server.shareClassPath#objects.sos.sosContent");
			}
			root.coop.init(
				configuration:thisRequest,
				sosContentObj:sosContent,
				api: API,
				attributes: root._init.attributes
			);
		}
		attributes.thisRequest = thisRequest;

		root._init = root.coop.onPageStart(root._init);
		if((structKeyExists(root,"attributes") && structKeyExists(root.attributes,"_postBack")) ||
			structKeyExists(form,"_postBack") ||
			structKeyExists(url,"_postBack") )
		{
			root._init = root.coop.onPostBack(root._init);
		} else {
			root._init = root.coop.onFirstCall(root._init);
		}
		root._init = root.coop.beforeViewCall(root._init);
		</cfscript>
		</cfcase><cfcase value="end">
		<cfscript>
		root.coop.onPageEnd(root._init);
		root.coop.sosContent.getHeadContent();
		</cfscript>
		</cfcase>
</cfswitch>
<cfsetting enablecfoutputonly="false">